package com.yungu.swift.order.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.lbs.model.DriverLocation;
import com.yungu.swift.lbs.model.vo.DrivingNearestVo;
import com.yungu.swift.lbs.service.DistanceService;
import com.yungu.swift.lbs.service.LocationService;
import com.yungu.swift.order.dao.OrderMapper;
import com.yungu.swift.order.model.vo.TripSharingInfoVo;
import com.yungu.swift.order.service.OrderTripSharingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * @author : cuixiuyin
 * @date : 2019/6/5
 */
@Service
public class OrderTripSharingServiceImpl implements OrderTripSharingService {

    @Autowired
    private OrderMapper orderMapper;

    @Reference
    private LocationService locationService;
    @Reference
    private DistanceService distanceService;


    @Override
    public ResponseData<TripSharingInfoVo> tripSharingInfo(String orderUuid, String appid) {
        TripSharingInfoVo tripSharingInfoVo = orderMapper.selectTripSharingInfo(orderUuid);
        if (null != tripSharingInfoVo) {
            List<DriverLocation> driverLocationList;
            DriverLocation lastLocation;
            tripSharingInfoVo.setPassengerStatus(6);
            //司机接单到乘客上车-等待接驾/待上车
            if (tripSharingInfoVo.getSubStatus() >= 210 && tripSharingInfoVo.getSubStatus() < 300) {
                driverLocationList = locationService.findOrderMapPoint(orderUuid, appid).getData();
                if (null == driverLocationList || driverLocationList.size() <= 0) {
                    return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取司机定位点失败！");
                }
                //取得最后一个点来作为当前的点
                lastLocation = driverLocationList.get(driverLocationList.size() - 1);
                tripSharingInfoVo.setDriverLocation(lastLocation);
                tripSharingInfoVo.setPassengerStatus(0);
            }
            //乘客上车-行程中
            if (tripSharingInfoVo.getSubStatus() == 300) {

                driverLocationList = locationService.findOrderDuringTheTripPoints(orderUuid, appid).getData();
                if (null == driverLocationList || driverLocationList.size() <= 0) {
                    return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取司机定位点失败！");
                }
                //取得最后一个点来作为当前的点
                lastLocation = driverLocationList.get(driverLocationList.size() - 1);
                tripSharingInfoVo.setDriverServiceProcessPointList(driverLocationList);
                tripSharingInfoVo.setPassengerStatus(1);
                //计算距离终点距离和到达终点预计行驶时间
                DrivingNearestVo drivingNearest = distanceService.getDrivingNearest(lastLocation.getCoordinate().getLng() + "",
                        lastLocation.getCoordinate().getLat() + "", tripSharingInfoVo.getDestLng() + "",
                        tripSharingInfoVo.getDestLat() + "").getData();
                //预估里程
                double distance = drivingNearest.getDistance() / 1000;
                //预估时长
                int duration = drivingNearest.getDuration() / 60;
                tripSharingInfoVo.setRemainingDistance(distance);
                tripSharingInfoVo.setEstimatedDrivingTime(duration);
            }
            //行程结束
            if ((tripSharingInfoVo.getSubStatus() >= 400 && tripSharingInfoVo.getSubStatus() <= 610)
                    || tripSharingInfoVo.getMainStatus() == 3
                    || tripSharingInfoVo.getMainStatus() == 4) {
                //判断行程是否在24小时前结束
                Date arriveTime = tripSharingInfoVo.getArriveTime();
                Date nowDate = new Date();
                if (nowDate.getTime() - arriveTime.getTime() >= (24 * 60 * 60 * 1000)) {
                    tripSharingInfoVo.setPassengerStatus(3);
                } else {
                    //到达目的地 -行程结束
                    driverLocationList = locationService.findOrderDuringTheTripPoints(orderUuid, appid).getData();
                    if (null == driverLocationList || driverLocationList.size() <= 0) {
                        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取司机定位点失败！");
                    }
                    tripSharingInfoVo.setDriverServiceProcessPointList(driverLocationList);
                    tripSharingInfoVo.setPassengerStatus(2);
                }
            }
            //该订单已取消
            if ((tripSharingInfoVo.getSubStatus() >= 900) && (tripSharingInfoVo.getSubStatus() <= 910)) {
                tripSharingInfoVo.setPassengerStatus(4);
            }
            //该订单已关闭
            if ((tripSharingInfoVo.getSubStatus() >= 980) && (tripSharingInfoVo.getSubStatus() <= 990)) {
                tripSharingInfoVo.setPassengerStatus(5);
            }
            return ResponseData.buildSuccessResponse("获取行程分享信息成功", tripSharingInfoVo);
        }
        return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "获取行程分享信息失败！");
    }
}
